#!/bin/bash
OLD_MYSQL_ROOT_PASSWD=`cat /var/log/mysqld.log | grep password | head -1 | rev  | cut -d ' ' -f 1 | rev`
MYSQL_ROOT_PASSWORD="123456"

# 1. 启动PHP
echo "Starting php..."
/usr/sbin/php-fpm -y /etc/php-fpm.conf -D

# 2. 启动 MongoDB
echo "Starting MongoDB..."
mongod &

# 3. 启动 Redis
echo "Starting Redis..."
redis-server /etc/redis/redis.conf &

# 4. Start MySQL
echo "Starting MySQL..."
# mysqld --skip-grant-tables --skip-networking --user=mysql  & 
mysqld --user=mysql  & 
count=0
while ! mysqladmin ping --silent; do
  count=$((count + 1))
  
  # 如果达到最大尝试次数，则退出并报错
  if [ $count -ge 10 ]; then
    echo "MySQL 启动失败，已达到最大重试次数!" 
    break
  fi
  
  echo "MySQL 尚未启动，正在尝试第 $count 次..."
  sleep 2
done 

#mysql 初始的旧密码
echo "OLD ROOT PWD: $OLD_MYSQL_ROOT_PASSWD"

#根据root密码是否为123456来判断是否已初始化容器
if ! mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "exit" 2>/dev/null; then
# if [ -d "/var/lib/mysql/mysql" ]; then
    echo "Is not initialized. Initializing MySQL..."
    # 初始化 MySQL 数据库
    mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql
    OLD_MYSQL_ROOT_PASSWD=`cat /var/log/mysqld.log | grep password | head -1 | rev  | cut -d ' ' -f 1 | rev`
   
    # 4. Start MySQL
    echo "Starting MySQL..." 
    mysqld --user=mysql  & 

    # 等待 MySQL 启动完成
    echo "等待 MySQL 启动..."
    while ! mysqladmin ping --silent; do
    sleep 1
    done 

    #修改 MySQL root 密码为 123456
    echo "update root pwd..." 
    mysql -u root -p"$OLD_MYSQL_ROOT_PASSWD" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD';FLUSH PRIVILEGES;"

    #创建用户
    echo "Create db user..."
    mysql -u"root" -p"$MYSQL_ROOT_PASSWORD" -e "CREATE USER 'sanguo'@'%' IDENTIFIED BY '$DB_USER_PWD';"
    mysql -u"root" -p"$MYSQL_ROOT_PASSWORD" -e "GRANT ALL PRIVILEGES ON *.* TO 'sanguo'@'%' WITH GRANT OPTION;"
    mysql -u"root" -p"$MYSQL_ROOT_PASSWORD" -e "FLUSH PRIVILEGES;" 

    #创建数据库
    echo "Create db..."
    mysql -u"root" -p"$MYSQL_ROOT_PASSWORD" -e "CREATE DATABASE IF NOT EXISTS sanguo;"
    #导入 sanguo.sql 数据
    echo "Import sql files..."
    if [ -f /home/sql/sanguo.sql ]; then
        mysql -u root -p"$MYSQL_ROOT_PASSWORD" "sanguo"  < /home/sql/sanguo.sql
    else
        echo "sanguo.sql 文件未找到，无法导入数据库"
    fi 
else
    echo "Is already initialized."   
fi

 echo "Update Files PUBLIC IP..."
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/common/settings.lua
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/web-mobile/dl/"new 2"
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/web-mobile/assets/main/index.bcc80.js
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/goserver/agent_admin/agent_assets/web/dist/js/highlight.js.0adc4c0f.js
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/goserver/admin/assets/web/dist/js/highlight.js.0adc4c0f.js
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/pyapi/wxpay/app.py
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/goserver/agent_admin/agent_assets/web/dist/js/app.bfd4e5a3.js
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/goserver/admin/assets/web/dist/js/app.3d18e251.js
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/web-mobile/assets/resources/import/24/241bc4ec-587f-41fa-a24d-e904c5f906c6.9f804.json
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/goserver/agent_admin/agent_assets/web/.env.development
    sed -i "s/66.66.66.66/${PUBLIC_IP}/g" /home/proj/goserver/admin/assets/web/.env.development
 
# 5. 进入 /home/proj/apiserver 执行 OpenResty 启动命令
cd /home/proj/apiserver
/usr/local/openresty/nginx/sbin/nginx -p `pwd` -c conf/nginx.conf &


# 6. 执行 skynet 相关命令
cd /home/proj/skynet
#加密
echo "Getkey..." 
sh getkey.sh
echo "启动登录服务..."
./skynet ../loginserver/prodconfig
echo "启动游戏服务..."
./skynet ../gameserver/prodconfig
echo "启动管理后台服务..."
cd /home/proj/goserver
nohup ./mygo &

# 启动 nginx 服务 
 /usr/sbin/nginx  -g "daemon off;"  
